<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>支付结果</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<style type="text/css">
			body,html{
				color:#1f1f1f;
				height: 100%;
				width: 100%;
				margin: 0;
				padding: 0;
				background-color: #f3f4f6;
			}
			.container{
				width: 100%;
				background-color: #FFFFFF;
			}
			.pay-result-box{
				display: flex;
				justify-content: center;
				align-items: center;
				padding-top:.94rem;
				height: 2rem;
			}
			#pay-state{
				display: inline-block;
				width: 1rem;
				height: 1rem;
				background-repeat: no-repeat;
				background-position: center;
				background-size: 70% 70%;
			}
			#pay-result{
				height: 1rem;
				line-height: 1rem;
				font-size: .45rem;
				padding-left: .15rem;
			}
			.wait{
				background-image: url(img/wait.png);
			}
			.success{
				background-image: url(img/success.png);
			}
			.failure{
				background-image: url(img/failure.png);
			}
			#pay-info{
				height: .6rem;
				line-height: .6rem;
				padding-bottom: .5rem;
				font-size: .35rem;
				text-align: center;
				color: #a7a7a7;
			}
			#ad{
				height: 1.4rem;
				width: 100%;
				position: fixed;
				bottom: 0;
				left: 0;
			}
		</style>
		<!--jquery-->
		<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>	
		 <!--解决ios延迟问题 -->
		<script src="./js/fastclick.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">$(function() {FastClick.attach(document.body);});</script>
		<!--全局管理-->
		<script src="js/base.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<div class="container">
			<div class="pay-result-box">	
				<span id="pay-state" class="wait"></span><span id="pay-result">处理中……</span>
			</div>		
			<div id="pay-info"></div>
			
			<div id="ad">
				<a id="ad-a" href="https://interaction.bayimob.com/gameHtml?appkey=31df29e7b75ed320bdf865b800e7ade9&adSpaceKey=ab70d3de93a199786572102ceaf23170&1=1">
					<img width="100%" height="100%" src="./img/ad.png"/>
				</a>
			</div>
		</div>
		<script type="text/javascript">
			// 接受url上的参数 判断支付结果！ out_trade_no
			// http://215h00638l.iask.in:12915/return.html?total_amount=0.10&timestamp=2018-08-16+15%3A08%3A24&sign=ccIiUNqrsXC5Z9KGMRF0TBT0NuLrxwXNfvzvj%2B7gsBBBqj9XkMM71nVwXHP8kX3%2BSj0IFwit0E9FVcLWXEU8DPJYQJdrhW%2FaFTNYRqJw8Ra2q55GlwCmg%2FCdHWi9LV4aZ9iQy%2BE4mCUCUq92AzoX3p3JsH1v%2F0ZpKslpjaTxpZJExYXFSvb0lxWjl8ni%2BTazbn5BBCoqJYsXhaf0NhJgDOQCqmo51aQ%2BbwrHedGNZbmJw8MJuvxaVkDKCo1iyxhPZONbOOZkZ1k1upY8hFZDgFcQ3MI4MQpjnxQjM9QZ8Pze5qfWaw1XJcZZ3HbTlp5YZPBXG2C48%2FGct5OFILnpnw%3D%3D&trade_no=2018081621001004090594400679&sign_type=RSA2&auth_app_id=2018051560140646&charset=utf-8&seller_id=2088131240467323&method=alipay.trade.wap.pay.return&app_id=2018051560140646&out_trade_no=201808160201115968786&version=1.0
			let $info = $('#pay-info');
			let $state = $('#pay-state');
			let $result = $('#pay-result');
			let baseUrl = window.baseUrl;
			$('#ad-a').attr('href',window.adUrl);

			// let out_trade_no = getQueryString('out_trade_no'); // 订单号
			// let total_amount = getQueryString('total_amount'); // 金额
			// let url = baseUrl + "paymentchannel/order/query?outTradeNo="+out_trade_no; // 获取状态接口
			// let flag = 30;
			
			let outTradeNo = getQueryString('out_trade_no'); // 订单号
			let resultCode = getQueryString('result_code'); // 
			let shopId = getQueryString('shopId'); // 
			let orderId = getQueryString('orderId'); // 
			let money = getQueryString('money'); //
			let type =  getQueryString('type'); //
			let timeEnd =  getQueryString('timeEnd'); //
			let sign =  getQueryString('sign'); //
			$info.html("本次消费共"+money+"元");
			let url = baseUrl + "channel/fast_silver/callback"; // 获取状态接口

			setTimeout(getPayState,500);
			
			function getPayState(){
				$.ajax({
					type:"get",
					url,
					data:{
						outTradeNo,resultCode,shopId,orderId,money,type,timeEnd,sign
					},
					async:true,
					success:function(res){
						handleKuaiYinResult(res);
						// if(res.resp_code === "000000"){
						// 	res.result && handleResult(res.result);
						// }else{
						// 	alert(res.resp_message);
						// }	
					},
					error:function(err){
						// console.log("获取支付结果失败")
						alert("服务器繁忙！");
					}
				});
			}
			
			// 新版快银支付
			function handleKuaiYinResult(result){
				// alert(JSON.stringify(result));
				if(result.resp_code === '000000'){
					$result.html(result.resp_message);
					$state.addClass("success");
				}else{
					$result.html(result.resp_message);
					$state.addClass("failure");
				}		
			}
			
			// 处理订单结果 【已弃用】
			/**
			 * {
			 * shopId:1882018080000001,
			 * shopName:"测试商户",
			 * outTradeNo:"018082302...10",
			 * totalFee:"0.1",
			 * orderType:"ALIPAY",
			 * tradeStatus:'WAIT_BUYER_PAY'
			 * }
			 * @param {Object} result
			 */
			function handleResult(result){
//				alert(JSON.stringify(result));
				// 金额打八折
				let total = result.totalFee>=1? Math.ceil(result.totalFee*80)/100 :result.totalFee;
				$info.html("本次消费"+ total +"元");
				if(result.tradeStatus ==="WAIT_BUYER_PAY"){
					// 创建中 继续查询
					$result.html("处理中……");
					$state.addClass("wait");
					flag--;
					if(flag>0){
						setTimeout(getPayState,1000);
					}else{
						alert("获取结果超时！");
					}
					
					return;
				}
				
				if(result.tradeStatus === "TRADE_SUCCESS"){
					// 支付成功
					$result.html("支付成功!");
					$state.addClass("success");
					return;
				}
				
				if(result.tradeStatus === "TRADE_FAILURE"){
					// 支付失败
					$result.html("支付失败!");
					$state.addClass("failure");
					return;
				}
				alert("意料之外的状态："+ result.tradeStatus);
			}
			
			// 获取url参数
			function getQueryString(name) {
			    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			    var r = window.location.search.substr(1).match(reg);
			    if (r != null) return unescape(r[2]); return null;
			}
			
		</script>
	</body>
	<script type="text/javascript">
		// 自适应字体大小
		(function () {
			var designW = 750;  //设计稿宽
			var font_rate = 100;
			//适配
			document.getElementsByTagName("html")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";
			document.getElementsByTagName("body")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";
	
			//监测窗口大小变化
			window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function () {
				document.getElementsByTagName("html")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";
				document.getElementsByTagName("body")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";
			}, false);
		})();		
	</script>
</html>
